package com.zyy.smartcity1129;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.Guideline;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {

    private ViewPager vpGuide;
    private Guideline guideline16;
    private LinearLayout ll;
    private Button btnToLogin;
    // 引导页适配器
    private GuideViewPagerAdapter adapter;
    // 引导页view列表
    private List<View> views = new ArrayList<View>();
    // 引导页图片Layout资源
    private static final int[] pics = { R.layout.guide_view1, R.layout.guid_view2,
            R.layout.guid_view3, R.layout.guid_view4, R.layout.guid_view5 };
    // 记录当前选中位置（上一位置）
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();

        // 初始化引导页view列表
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);
//            最后一页按钮点击跳转登录页
            if (i == pics.length - 1) {
                btnToLogin = (Button) view.findViewById(R.id.btnToLogin);
                btnToLogin.setOnClickListener(view1 -> {
                    startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                    finish();
                });
            }
            views.add(view);
        }

        // 初始化adapter
        adapter = new GuideViewPagerAdapter(views);
        vpGuide.setAdapter(adapter);

//    ViewPager滑动监听器 https://blog.csdn.net/xipiaoyouzi/article/details/12121131
        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

//       页面跳转完后调用
            @Override
            public void onPageSelected(int position) {
//           对5取余得到当前页数
                int newsPoint = position % 5;
//          设置上一个页面的point
                ll.getChildAt(currentIndex).setBackgroundResource(R.drawable.radiobutton_off_background);
//          设置当前页面的point
                ll.getChildAt(newsPoint).setBackgroundResource(R.drawable.radiobutton_on_background);
//          将当前页数存为上一页数
                currentIndex = newsPoint;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//      初始化小点
        initPoint();
    }

    private void initPoint() {
        // 循环取得小点图片
        View pointView;
        for (int i = 0; i < pics.length; i++) {
            // 得到一个LinearLayout下面的每一个子元素
            pointView = new View(GuideActivity.this);
            pointView.setBackgroundResource(R.drawable.radiobutton_off_background);
//            小点尺寸
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(25, 25);
            if (i != 0) {
                layoutParams.leftMargin = 10;
            }
            ll.addView(pointView, layoutParams);
        }
//        默认值
        ll.getChildAt(0).setBackgroundResource(R.drawable.radiobutton_on_background);
        currentIndex = 0;
    }

    private void initView() {
        vpGuide = (ViewPager) findViewById(R.id.vp_guide);
        guideline16 = (Guideline) findViewById(R.id.guideline16);
        ll = (LinearLayout) findViewById(R.id.ll);
    }

//    PagerAdapter  https://blog.csdn.net/harvic880925/article/details/38487149
    class GuideViewPagerAdapter extends PagerAdapter {
        private List<View> views;

//        图片列表
        public GuideViewPagerAdapter(List<View> views) {
            this.views = views;
        }

        @Override
        public int getCount() {
            if (views != null) {
                return views.size();
            }
            return 0;
        }

//        一个Page在切换完成后会调用该方法去加载下一个即将展示的Page
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(views.get(position), 0);
            return views.get(position);
        }

//        销毁隔着一个page的view
        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == ((View) object);
        }
    }
}